import UsersApi from "../api/user/user";
import _ from "lodash";
export default {
  namespaced: true,
  state: {
    curPage: 1,
    eachPage: 3,
    total: 0,
    maxPage: 0,
    rows: [],
  },
  mutations: {
    get(state, data) {
      _.assign(state, data);
    },
    setCurPage(state, curPage) {
      state.curPage = curPage;
    },
    async updates(state, data) {
      const { id, phone, address } = data;
      const datas = await UsersApi.updateUser(id, phone, address);
      return datas.state;
    },
    async addUsers(state, data) {
      const { username, password, role, address, phone } = data;
      await UsersApi.addUser({ username, password, role, address, phone });
    },
    async searchUsers(state, username) {
      const { curPage, eachPage } = state;
      const { data } = await UsersApi.searchUser(username, curPage, eachPage);
      _.assign(state, data);
    },
  },
  actions: {
    get: async ({ state, commit }) => {
      const { curPage, eachPage } = state;
      const data = await UsersApi.get(curPage, eachPage);
      commit("get", data);
    },
  },
};
